package com.cykj.admin.service.impl;

import com.cykj.admin.mapper.EnterpriseMapper;
import com.cykj.admin.service.IEnterpriseService;
import com.cykj.admin.vo.HRVO;
import com.cykj.admin.vo.IndustryVO;
import com.cykj.common.dto.ResponseDTO;
import com.cykj.common.entity.HrDO;
import com.cykj.common.entity.IndustryDO;
import com.cykj.common.entity.RecruitInfoDO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject:@BelonqsPackage:com.cykj.admin.service.impl
 * @Author:jiangna
 * @CreateTime:2025-08-16 11:27
 */
@Service
public class EnterpriseServiceImpl implements IEnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Value("${fileUrl}")
    private String httpPath;
    @Value("${localPath}")
    private String localPath;

    /**
     * @Description: 查询所有行业的信息/根据条件查询行业信息
     * @Param: [industryVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO selectByIndustry(IndustryVO industryVO) {
        List<IndustryDO> industryList = enterpriseMapper.selectByIndustry(industryVO);
        if (industryList != null && !industryList.isEmpty()) {
            return ResponseDTO.success(industryList);
        } else {
            return ResponseDTO.error("未获取到该行业数据");
        }
    }

    /**
     * @Description: 添加行业
     * @Param: [industryVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO addIndustry(IndustryVO industryVO) {
        int result = enterpriseMapper.addIndustry(industryVO);
        if (result > 0) {
            return ResponseDTO.success("添加行业成功");
        } else {
            return ResponseDTO.error("添加行业失败");
        }
    }

    /**
     * @Description: 删除行业（拉黑 → 改状态）
     * @Param: [industryVO]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    public ResponseDTO deleteIndustry(Integer industryId) {
        int result = enterpriseMapper.deleteIndustry(industryId);
        if (result > 0) {
            return ResponseDTO.success("删除行业成功");
        } else {
            return ResponseDTO.error("删除行业失败");
        }
    }

    /**
     * @Description: 查询所有的HR的信息 / 根据所属企业 账号查询
     * @Param: [hrvo]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO selectByHr(HRVO hrvo) {
        PageInfo<HrDO> page;
        //开启分页
        PageHelper.startPage(hrvo.getCurrentPage(), hrvo.getPageSize());
        List<HrDO> hrList = enterpriseMapper.selectByHr(hrvo);
        page = new PageInfo<>(hrList);
        if (page.getTotal() < page.getPageSize() && hrList.size() == 0 && page.getTotal() > 0) {
            hrvo.setCurrentPage(1);
            PageHelper.startPage(hrvo.getCurrentPage(), hrvo.getPageSize());
            List<HrDO> hrList2 = enterpriseMapper.selectByHr(hrvo);
            page = new PageInfo<>(hrList2);
        }
        return ResponseDTO.success(page);
    }

    /**
     * @Description: 重置HR密码
     * @Param: [hrvo]
     * @return: com.cykj.common.dto.ResponseDTO
     */
    @Override
    public ResponseDTO resetPassword(Integer hrId) {
        int result = enterpriseMapper.resetPassword(hrId);
        if (result > 0){
            return  ResponseDTO.success(result);
        }
        return ResponseDTO.error("HR密码重置失败");
    }

    @Override
    public ResponseDTO deleteHR(HRVO hrvo) {
        int result = enterpriseMapper.deleteHR(hrvo);
        if (result > 0){
            return ResponseDTO.success(result);
        }
        return ResponseDTO.error("拉黑失败，检查账号是否存在！");
    }

    /**
     * @Description: 路径转换
     * @Param: [fileName]
     * @return: java.lang.String
     */
    private String changeUrl(String fileName) {
        String staticFile = fileName.replace(httpPath, localPath);
        return staticFile.replace("file:", "");
    }
}
